import { Application } from 'egg';
import {
  indexRule,
  createRule,
  updateRule,
  destroyRule,
  forTplRule
} from '../validate/carousel';

/**
 * 轮播图片管理
 */
export default (app: Application) => {
  const { auth } = app.middleware;
  const { jwt } = app;

  return {
    'pic.carousel.list': {
      method: 'GET',
      path: '/api/pic/carousel',
      options: {
        tags: ['carousel/list'],
        description: '轮播图列表',
        handler: app.controller.pic.carousel.list,
        middlewares: [jwt, auth('pic.carousel.list')],
        validate: indexRule,
      },
    },
    'pic.carousel.forTpl': {
      method: 'GET',
      path: '/api/pic/carousel/tpl',
      options: {
        tags: ['carousel/tpl'],
        description: '查询模板设计里面的banner',
        handler: app.controller.pic.carousel.getBannersForTemplate,
        middlewares: [jwt],
        validate: forTplRule,
      },
    },
    'pic.carousel.create': {
      method: 'POST',
      path: '/api/pic/carousel',
      options: {
        tags: ['carousel/create'],
        description: '创建轮播图',
        handler: app.controller.pic.carousel.create,
        middlewares: [jwt, auth('pic.carousel.create')],
        validate: createRule,
      },
    },
    'pic.carousel.update': {
      method: 'PUT',
      path: '/api/pic/carousel/:id',
      options: {
        tags: ['carousel/update'],
        description: '更新轮播图',
        handler: app.controller.pic.carousel.update,
        middlewares: [jwt, auth('pic.carousel.update')],
        validate: updateRule,
      },
    },
    'pic.carousel.destroy': {
      method: 'DELETE',
      path: '/api/pic/carousel/:id',
      options: {
        tags: ['carousel/delete'],
        description: '删除轮播图',
        handler: app.controller.pic.carousel.destroy,
        middlewares: [jwt, auth('pic.carousel.destroy')],
        validate: destroyRule,
      },
    }
  };
};
